
package fhv.itb.ec.soap;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.jws.WebService;
import javax.ejb.Stateless;


@WebService()
@Stateless()
public class SOAPServer {

    private File m_root;
    private File m_dir;


    public SOAPServer() {
        m_root = new File("/Users/Malcolm/School/SEM5/Enterprise_Computing/" +
                "Assignments/Ex8/tmpsrv");
        m_dir = m_root;
    }


    public String getDir() {

        if (m_dir.getPath().equals(m_root.getPath())) {
            return File.separator;
        }

        return new String(m_dir.getPath().
                substring(m_root.getPath().length()));
    }


    public boolean changeDir(String dir) {
        File newDir = new File(m_dir.getPath() + File.separator + dir);

        if (dir.equals("..")) {

            if (!m_dir.getPath().equals(m_root.getPath())) {
                m_dir = new File(m_dir.getParent());
                return true;
            }

        } else if (newDir.isDirectory()) {
            m_dir = newDir;
            return true;
        }

        return false;
    }


    public String list() {
        StringBuffer result = new StringBuffer();
        File[] files = m_dir.listFiles();

        for (File f : files) {

            if (f.isDirectory()) {
                result.append(f.getName() + " *\n");
            } else {
                result.append(f.getName() + "\n");
            }

        }

        return new String(result);
    }


    public boolean write(DataHandler dh, String fileName) {
        File out = new File(m_dir.getPath() + File.separator + fileName);

        if (!out.exists()) {

            try {
                out.createNewFile();
            } catch (IOException ex) {
                System.out.println("Could not create file " + fileName + "to put on server");
                Logger.getLogger(SOAPServer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }


        try {
            FileOutputStream os = new FileOutputStream(out);
            dh.writeTo(os);
            return true;
        } catch (FileNotFoundException ex) {
            System.out.println("Could not create file " + fileName + "to put on server");
            Logger.getLogger(SOAPServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            System.out.println("Could not create file " + fileName + "to put on server");
            Logger.getLogger(SOAPServer.class.getName()).log(Level.SEVERE, null, ex);
        }


        return false;
    }


    public DataHandler read(String fileName) {
        File f = new File(m_dir.getPath() + File.separator + fileName);
        FileDataSource fds = null;
        DataHandler dh = null;

        System.out.println("Trying to get file " + f.getPath());
        if (f.exists()) {
            fds = new FileDataSource(f);
            dh = new DataHandler(fds);
        }

        return dh;
    }
}
